#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 0;    //角度


#include <IRremote.h>  //including infrared remote header file     
int RECV_PIN = 2; // the pin where you connect the output pin of IR sensor
int led = 3 ;// led
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn();
  digitalWrite (led, 0);
  myservo.attach(9);  //舵机9号口
}

bool button_0 = 0; //按钮1状态
bool button_4 = 0;//按钮4状态
void loop()
{
  if (irrecv.decode(&results))// Returns 0 if no data ready, 1 if data ready.
  {
    if (4294967295 !=  results.value) {
      Serial.print("Code: ");
      Serial.println(results.value); //prints the value a a button press

      if (results.value == 33480735) { // 0
        Serial.println("0 is push");
        button_0 = !button_0;
        if (button_0) {
          digitalWrite (led, 1);
        }
        else {
          digitalWrite (led, 0);
        }

      }
      if (results.value == 33444015) {
        Serial.println("1 is push");
      }
      if (results.value == 33478695 ) {
        Serial.println("2 is push");
      }
      if (results.value == 33486855) {
        Serial.println("3 is push");
      }
      if (results.value == 33435855) {
        Serial.println("4 is push");
        button_4 = !button_4;

        if (button_4) {
          digitalWrite (led, 1);
        }
        else {
          digitalWrite (led, 0);
        }

      }

      if (button_0) { //集控
        if (results.value == 33439935) {
          Serial.println("< is push");
          if (pos <= 0 ) {
            pos = 0;
          }
          else {
            pos -= 10;
          }
          myservo.write(pos);
        }
        if (results.value == 33472575) {

          Serial.println("> is push");
          if (pos >= 180 ) {
            pos = 180;

          }
          else {
            pos += 10;
          }
          myservo.write(pos);
        }

      }//集控

      else { //单控 需要按键

        if (button_4) { //按下4之后确认状态 1
          digitalWrite (led, 1);
          if (results.value == 33439935) {
            Serial.println("< is push");
            if (pos <= 0 ) {
              pos = 0;
            }
            else {
              pos -= 10;
            }
            myservo.write(pos);
          }
          if (results.value == 33472575) {

            Serial.println("> is push");
            if (pos >= 180 ) {
              pos = 180;

            }
            else {
              pos += 10;
            }
            myservo.write(pos);
          }

        } //单控 需要按键

        else {
          digitalWrite (led, 0);
        }

      }

    }
    irrecv.resume(); // 释放红外等待下一个值
  }


  
}